package guzb.cnblogs.classloader;

import guzb.cnblogs.classloader.thirdapi.v1.DeviceBasicInfoV1;
import guzb.cnblogs.classloader.thirdapi.v1.DeviceFactoryV1;
import guzb.cnblogs.classloader.thirdapi.v2.DeviceBasicInfoV2;
import guzb.cnblogs.classloader.thirdapi.v2.DeviceFactoryV2;

/**
 * 演示如何同时加载多个相同名称的Jar包
 *
 * @author 顾志兵
 * @mail ipiger@1314
 * @since 2024-05-18
 */
public class LoadNameConflictClassesAppMain {

    public static void main(String[] args) throws Exception {
        normalTest();
    }

    private static void normalTest() throws Exception{
        // 调用第三方接口的第一版本（jar1, 被包装成了 jar1wrapper)
        String third1WrapperJarPath = "/third-lib/third-provider-jar1-wrapper-1.0.0.jar";
        ClasspathJarLoader third1Classloader = new ClasspathJarLoader(third1WrapperJarPath);
        Class third1DeviceFactoryClass = third1Classloader.loadClass("guzb.cnblogs.classloader.third1wrapper.DeviceFactoryV1Impl");
        DeviceFactoryV1 deviceFactoryV1 = (DeviceFactoryV1)third1DeviceFactoryClass.newInstance();
        DeviceBasicInfoV1 device1 = deviceFactoryV1.getDeviceInfo("BN8964");
        System.out.println("第三方接口v1版本的调用结果:");
        System.out.println(device1.toString());

        System.out.println();

        // 调用第三方接口的第二版本（jar2, 被包装成了 jar2wrapper)
        String third2WrapperJarPath = "/third-lib/third-provider-jar2-wrapper-1.0.0.jar";
        ClasspathJarLoader third2Classloader = new ClasspathJarLoader(third2WrapperJarPath);
        Class third2DeviceFactoryClass = third2Classloader.loadClass("guzb.cnblogs.classloader.third2wrapper.DeviceFactoryV2Impl");
        DeviceFactoryV2 deviceFactoryV2 = (DeviceFactoryV2)third2DeviceFactoryClass.newInstance();
        DeviceBasicInfoV2 device2 = deviceFactoryV2.getDeviceInfo("BN8633");
        System.out.println("第三方接口v2版本的调用结果:");
        System.out.println(device2.toString());
    }

}
